package com.tutu.common;

import java.util.Calendar;
import java.util.Collection;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.SortedSet;
import java.util.TreeSet;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.Assert;

import com.tutu.common.util.JsonReturnObject;
import com.tutu.user.entity.SysUser;

/**
 * 该类定义项目中全局常量。
 */
public class GlobalConstants {
	
	/** 
	 * 默认页面每页显示多少记录 
	 */
    public static final int DEFAULT_PAGE_NUMBER = 10;
	
	/**
	 * 激活状态 [1]
	 */
	public static final String FLAG_ENABLE = "1";
	/**
	 * 禁用状态 [0]
	 */
	public static final String FLAG_DISENABLE = "0";
	/**
	 * 保护状态[3]
	 */
	public static final String FLAG_PROTECT = "3";
	/**
	 * 默认密码
	 */
	public static final String DEFAULT_PASSWORD = "111111";
	
	public static final String SYSTEM_USER = "meoa";

	public static final String WATER_TOKEN_IDENTITY = "tutu_meoa_token";
	
	/** 上传文件的类型 **/
	public static final String DATA_DIC_UPLOAD_TYPE = ".jpeg|.gif|.jpg|.png|.bmp|.pic|";
	/** 上传文件的最大大小 **/
	public static final int DATA_DIC_UPLOAD_MAX_SIZE = 5;
	/**
	 * SESSION中存放用户KEY
	 */
	public static final String SESSION_USER_INFO = "SESSION_USER_INFO";
	/**
	 * SESSION中存放用户菜单
	 */
	public static final String SESSION_USER_MENU = "SESSION_USER_MENU";
	/**
	 * SESSION中存储错误信息键
	 */
	public static final String SESSION_MESSAGE = "SESSION_MESSAGE";
	/**
	 * 系统用户ID
	 */
	public static final String SYSTEM_USER_ID = "65265583-FC4D-413B-8D4D-0FCDDFCBBAA4";
	/**
	 * 文件存放路径
	 */
	public static final String FILE_STORE_PATH = "FILE_STORE_PATH";
	/**
	 * 流程发布人
	 */
	public static final String APPLY_USER = "APPLY_USER";
	/**
	 * 表单流程KEY
	 */
	public static final String FLOW_FROM_VAR = "FLOW_FROM_VAR";
	
	/**
	 * 生成交易单据编号
	 * @return
	 */
	public synchronized static String createTradeBillNumber(){
		return RandomStringUtils.random(1,"123456789") + RandomStringUtils.randomNumeric(1) + DateFormatUtils.format(Calendar.getInstance().getTime(), "SssmmHHddMMyyyy");
	}
	
	/**
	 * 树状中叶子节点
	 * @author qiang.tu
	 * 2014-4-11
	 */
	public interface IsLeafNode{
		/**
		 * 非叶子节点
		 */
		String NOT_LEAF_NODE = "0";
		/**
		 * 叶子节点
		 */
		String LEAF_NODE = "1";
	}
	
	/**
	 * 系统状态标识映射
	 * @return
	 */
	public static Map<String, String> getSysStateMap(){
		Map<String, String> map = new HashMap<String, String>();
		map.put(FLAG_ENABLE, "成功");
		map.put(FLAG_DISENABLE, "失败");
		return map;
	}
	
	public static SysUser getCurrentUser(){
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		SysUser sysUser = null;
		if(null!=authentication){
			Object object = authentication.getPrincipal();
			if(object instanceof SysUser){
				sysUser = (SysUser) object;
			}
		}
		return sysUser;
	}
	
	/**
	 * 保存前台展示消息
	 * @param session
	 * @param jsonReturnObject
	 */
	public static void setSessionMessage(HttpSession session, JsonReturnObject<String> jsonReturnObject){
		session.setAttribute(GlobalConstants.SESSION_MESSAGE, jsonReturnObject);
	}
	
	/**
	 * 保存前台展示消息
	 * @param session
	 * @param jsonReturnObject
	 */
	public static ISessionMessageSetting setSessionMessage(HttpSession session, String failed, String success){
		ISessionMessageSetting sessionMessageSetting = new GlobalConstants().new SessionMessageSetting(session, failed, success);
		return sessionMessageSetting;
	}
	
	public interface ISessionMessageSetting{
		void setIsSuccess(boolean isSuccess);
	}
	
	public class SessionMessageSetting implements ISessionMessageSetting{
		
		JsonReturnObject<String> jsonReturnObject = new JsonReturnObject<String>();
		
		public SessionMessageSetting(HttpSession session, String failed, String success){
			jsonReturnObject.setIsSuccess(false);
			jsonReturnObject.setErrorMsg(failed);
			jsonReturnObject.setReturnObject(success);
			session.setAttribute(GlobalConstants.SESSION_MESSAGE, jsonReturnObject);
		}

		@Override
		public void setIsSuccess(boolean isSuccess) {
			jsonReturnObject.setIsSuccess(isSuccess);
		}
		
	}
	
	/**
	 * 清除Session所有信息
	 * 
	 * @param session
	 *            HttpSession
	 */
	public static void removeAllAttributes(HttpSession session) {
	    if (session == null)
	        return;
	    Enumeration<String> enu = session.getAttributeNames();
	    while (enu.hasMoreElements()) {
	        session.removeAttribute(enu.nextElement());
	    }
	}
	
	/**
	 * 对集合排序
	 * @param authorities
	 * @param comparator
	 * @return
	 */
	public static <T> SortedSet<T> sortAuthorities(Collection<? extends T> authorities,Comparator<T> comparator){
		Assert.notNull(authorities, "Cannot pass a null GrantedAuthority collection");
		SortedSet<T> sortedAuthorities = new TreeSet<T>(comparator);
		for (T grantedAuthority : authorities) {
			Assert.notNull(grantedAuthority, "GrantedAuthority list cannot contain any null elements");
			sortedAuthorities.add(grantedAuthority);
		}
		return sortedAuthorities;
	}
	
}
